<?php

// use Illuminate\Support\Facades\Schema;
use Jialeo\LaravelSchemaExtend\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class InitProduct extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('products', function (Blueprint $table) {
            $table->comment = '商品表';
            
            $table->increments('id');
            $table->uuid('uuid')->index();

            $table->integer('shop_id')->unsigned()->nullable()->index();  // 商户id

            // 品牌/系列/分类
            // $table->integer('series_id')->unsigned()->nullable()->index();
            $table->integer('brand_id')->unsigned()->nullable()->index()->comment('品牌id');
            $table->string('category_ids', 255)->nullable()->comment('分类id数组');

            $table->string('title', 100)->index()->comment('标题');
            $table->string('sub_title', 100)->nullable()->comment('副标题');
            $table->string('thumb', 255)->nullable()->comment('封面');
            $table->string('poster', 255)->nullable()->comment('分享海报');  // 分享海报

            $table->string('description', 255)->nullable()->comment('商品小描述');  // 商品小描述

            $table->string('tags', 255)->nullable()->comment('标签');
            $table->char('number', 50)->nullable()->index()->comment('编号');
            $table->char('ean')->nullable()->comment('ean码');

            $table->integer('cost_price')->unsigned()->nullable()->comment('成本价');   // 成本价


            $table->integer('discount_money_price')->unsigned()->comment('促销人民币价')->nullable();   // 展示价格
            $table->integer('discount_score_price')->unsigned()->comment('促销积分价')->nullable();   // 展示价格

            $table->integer('money_price')->unsigned()->comment('人民币价格')->nullable()->index();   // 展示价格
            $table->integer('score_price')->unsigned()->comment('积分价格')->nullable();   // 展示价格

            $table->integer('brokerage_money')->unsigned()->nullable()->comment('推广红包佣金');   // 售价
            $table->integer('brokerage_score')->unsigned()->nullable()->comment('推广积分佣金');   // 售价
            $table->integer('second_brokerage_money')->unsigned()->nullable()->comment('上级推广人红包佣金');   // 售价
            $table->integer('second_brokerage_score')->unsigned()->nullable()->comment('上级推广人积分佣金');   // 售价
            $table->integer('reward_money')->unsigned()->nullable()->comment('商品奖励红包');  
            $table->integer('reward_score')->unsigned()->nullable()->comment('商品奖励积分'); 

            // $table->integer('groupon_id')->unsigned()->nullable()->index();
            // $table->integer('seckill_id')->unsigned()->nullable()->index();
            // $table->integer('bargain_id')->unsigned()->nullable()->index(); // 

            // 发货说明
            // $table->tinyInteger('type')->default(0)->index();  // 0为实物   1为虚拟商品
            // $table->tinyInteger('freight_type')->default(0);  // 0为物流发货   1为线上发货
            // $table->boolean('is_auto_freight')->default(0);

            // 开售时间

            // $table->boolean('is_limit_sell_time')->default(0);   // 是否限时开售
            // $table->timestamp('start_sell_at')->nullable();      // 开售时间
            // $table->timestamp('end_sell_at')->nullable();      // 开售时间

            // 限制购买件数
            // $table->boolean('is_limit_buy_amount')->default(0);   // 是否限制购买数量
            // $table->tinyInteger('limit_buy_amount')->default(1);  // 限制购买数量

            // 运费
            // $table->boolean('is_freight_free')->default(1);   // 是否包邮
            $table->tinyInteger('carriage_type')->default(0)->comment('0为按统一设置  1为包邮  2为到付  3为单独设置');   // 0为包邮  1为到付  2为其它运费模板
            $table->integer('carriage_template_id')->unsigned()->index()->nullable()->comment('费模板'); // 运费模板

            // 预售
            // $table->boolean('is_presell')->default(0);   // 是否预售
            // $table->char('presell_detail', 255)->nullable();  // 限制发货时间说明

            $table->boolean('is_show_stock')->default(1)->comment('是否显示库存');    // 是否显示库存   
            $table->boolean('is_show_sales')->default(1)->comment('是否显示销量');    // 是否显示销量

            $table->boolean('is_recommend')->default(0)->index()->comment('是否推荐款');    // 是否推荐款

            $table->boolean('is_listed')->default(1)->index()->comment('是否显示在列表中');  // 是否显示在列表中
            $table->integer('list_weight')->default(99)->index()->comment('列表中权重'); // 列表中权重

            $table->integer('stock')->default(0)->index()->comment('库存'); // 列表中权重
            $table->integer('sales')->default(0)->index()->comment('销量'); // 列表中权重

            $table->tinyInteger('status')->default(0)->index()->comment('0为未上架  1为上架中'); // 0为未上架  1为上架中 

            $table->string('remark', 255)->nullable()->comment('备注');

            $table->timestamp('published_at')->nullable()->comment('发布时间');

            $table->softDeletes();
            $table->timestamps();
        });

        Schema::create('product_exts', function (Blueprint $table) {
            $table->comment = '商品扩展表';
            
            $table->increments('id');
            $table->uuid('uuid')->index();

            $table->integer('product_id')->unsigned()->index()->comment('商品id');     //所属商品

            $table->text('images', 10000)->nullable()->comment('详情图');
            $table->text('videos', 10000)->nullable()->comment('视频');
            $table->text('sku_attrs')->comment('sku属性');
            $table->text('header_attrs')->nullable()->comment('显示在标题正文的属性'); // 显示在标题正文的属性
            $table->text('normal_attrs')->nullable()->comment('普通属性'); // 普通属性

            $table->longText('page')->nullable()->comment('详情页 html 如果有'); // 所有属性

            $table->softDeletes();
            $table->timestamps();

            // $table->foreign('product_id')->references('id')->on('products')
            //     ->onUpdate('cascade')->onDelete('cascade'); 
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::disableForeignKeyConstraints(); 
        Schema::dropIfExists('product_exts');
        Schema::dropIfExists('products');
    }
}
